﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DbUpgradeDameng.General
{
    public static class Expand
    {
        #region 对象转为字符串
        /// <summary>
        /// 对象转为字符串
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static string ToStringEx(this object o)
        {
            if (o == null)
            {
                return string.Empty;
            }
            else
            {
                return o.ToString();
            }
        }
        #endregion

        #region 对象转为字符串(null不转化)
        /// <summary>
        /// 对象转为字符串(null不转化)
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static string ToStringNullEx(this object o)
        {
            if (o == null)
            {
                return null;
            }
            else if (o == System.DBNull.Value)
            {
                return null;
            }
            else
            {
                return o.ToString();
            }
        }
        #endregion

        #region 字符串转INT32

        /// <summary>
        /// 字符串转INT32
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static int ToInt32Ex(this object o)
        {
            string so = o.ToStringEx();
            int i = 0;
            int.TryParse(so, out i);
            return i;

        }
        #endregion

        #region 字符串转INT64

        /// <summary>
        /// 字符串转INT64
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static long ToInt64Ex(this object o)
        {
            string so = o.ToStringEx();
            long i = 0;
            Int64.TryParse(so, out i);
            return i;

        }
        #endregion

        #region 转为可为空日期
        /// <summary>
        /// 转为可为空日期
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static DateTime? ToDateTimeNullEx(this object o)
        {
            string so = o.ToStringEx();
            DateTime dt;
            if (o == null || o == System.DBNull.Value)
            {
                return null;
            }
            else
                if (DateTime.TryParse(so, out dt))
            {
                return dt;
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region 转为日期
        /// <summary>
        /// 转为日期
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static DateTime ToDateTimeEx(this object o)
        {
            string so = o.ToStringEx();
            DateTime dt;
            if (DateTime.TryParse(so, out dt))
            {
                return dt;
            }
            else
            {
                return DateTime.Parse("2000-01-01 00:00:00");
            }
        }
        #endregion
    }
}
